package com.citywithincity.db;

import com.citywithincity.utils.OnlineState;

public class AbsDao<T> {
	
	private Class<T> clazz;
	private Entity<T> entity;
	
	private String state;
	
	public AbsDao(Class<T> clazz,String state){
		entity = EntityBuilder.getEntity(clazz);
		this.clazz = clazz;
		this.state = state;
	}
	
	/**
	 * 上线
	 */
	public int online(Object id){
		return Db.ar().
		set(state,OnlineState.ONLINE)
		.where(entity.pk,id)
		.where(state,Symbos.neq, OnlineState.ONLINE)
		.update(entity.tableName);
	}
	
	
	/**
	 * 下线
	 * @return
	 */
	public int offline(Object id){
		return Db.ar().
				set(state,OnlineState.OFFLINE)
				.where(entity.pk,id)
				.where(state,Symbos.neq, OnlineState.OFFLINE)
				.update(entity.tableName);
	}
	
	/**
	 * 增加
	 * @param data
	 * @return
	 */
	public int add(T data){
		int ret = Db.ar(clazz).insert(data);
		return ret;
	}
	
	
	
	public int update(T data){
		int ret = Db.ar(clazz).update(data,true);
		
		return ret;
	}
	
	
	
	public int delete(Object id){
		int ret = Db.ar(clazz).where(entity.pk,id).delete();
		
		return ret;
	}
	


	
}
